package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.MerchantInfo;
import com.contest.util.IdWorker;
import com.contest.util.MD5Utils;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.contest.entity.UserInfo;
import com.contest.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author liuyf
 * @email 123456@qq.com
 * @date 2018-01-19 14:16:33
 */

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param, HttpServletRequest request) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        String username = String.valueOf(param.get("name"));
        Page<UserInfo> mPage = new Page<UserInfo>(page, rows);
        MerchantInfo ui = (MerchantInfo) request.getSession().getAttribute("user");
        Wrapper<UserInfo> w = new EntityWrapper();
        w.addFilter("fd_merchant={0}",ui.getFdId());
        if(username != null && username.trim().length() > 0 && !"null".equals(username)){
            w.addFilter("fd_nickname={0}",username);
        }
        mPage = userInfoService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(UserInfo userInfo,HttpServletRequest request) {

        Wrapper<UserInfo> w = new EntityWrapper<>();
        w.addFilter("fd_nickname={0}",userInfo.getFdNickname());
        List<UserInfo> list = userInfoService.selectList(w);
        if(list.size() > 0){
            return R.error("用户昵称已存在");
        }

        Wrapper<UserInfo> w2 = new EntityWrapper<>();
        w2.addFilter("fd_mobile={0}",userInfo.getFdMobile());
        List<UserInfo> list2 = userInfoService.selectList(w2);
        if(list2.size() > 0){
            return R.error("用户昵称已存在");
        }

        userInfo.setFdId(idWorker.nextId() + "");
        userInfo.setFdRoleId("2");
        userInfo.setFdPwd(MD5Utils.encrypt(userInfo.getFdPwd()));
        userInfo.setFdRechargeTotal(new BigDecimal(0));
        userInfo.setFdBettingTotal(new BigDecimal(0));
        userInfo.setFdOver(new BigDecimal(0));
        MerchantInfo ui = (MerchantInfo) request.getSession().getAttribute("user");
        userInfo.setFdMerchant(ui.getFdId());
        userInfo.setFdMerchantName(ui.getFdName());
        userInfo.setFdRecommendCode(ui.getFdMyCode());
        userInfo.setFdRoleName("普通用户");
        if (userInfoService.insert(userInfo)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        UserInfo userInfo = userInfoService.selectById(fdId);
        model.addAttribute("userInfo", userInfo);
        return "userInfo/edit";
    }


    @PostMapping("/updatePwd")
    @ResponseBody
    public R updatePwd(String id) {
        String pwd = MD5Utils.encrypt("123456");
        UserInfo userInfo = userInfoService.selectById(id);
        userInfo.setFdPwd(pwd);
        boolean b = userInfoService.updateById(userInfo);
        if(b){
            return R.ok("操作成功！重置密码为：123456");
        }else{
            return R.error("重置失败！");
        }
    }



    @PostMapping("/userRecharge")
    @ResponseBody
    public R userRecharge(String amount,String userid,HttpServletRequest request) {
        if(amount == null || userid == null){
            return R.error("输入参数不能为空！");
        }
        boolean b = userInfoService.userRecharge(amount,userid,request);
        if(b){
            return R.ok("充值成功！金额为：" + amount);
        }else{
            return R.error("充值失败！");
        }
    }


    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (userInfoService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }
    @GetMapping("/view/{fdId}")
    public String view(Model model, @PathVariable("fdId") String fdId) {
        UserInfo userInfo = userInfoService.getUserDetailByUserId(fdId);
        model.addAttribute("userInfo", userInfo);
        return "userInfo/view";
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(UserInfo userInfo) {
        if (userInfoService.updateById(userInfo)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

}
